package com.lmmob.autotest.uitest.controls;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class Window extends CompositeControl {
    private String title;

    public Window(String title, WebDriver webDriver) {
        super(webDriver);
        this.title = title;
        waitForLoaded();
    }

    private void waitForLoaded() {
        findElement(By.xpath(String.format("//*[text()='%s']", this.title)));
    }

    @Override
    protected String getSelfQuery() {
        return String.format(".query(\"window[title='%s']\")[0]", title);
    }

    public void close() {
        String btnCloseId = (String) runScript("return " + this.getQuery() + ".query(\"[type='close']\")[0].id");
        WebElement btnClose = findElement(By.id(btnCloseId)).findElement(By.tagName("img"));
        btnClose.click();
    }

    @Override
    protected String getName(){
        return this.title;
    }
}
